from PySide6.QtCore import QSize, Qt
from PySide6.QtWidgets import QMainWindow, QPushButton, QHBoxLayout, QVBoxLayout, QWidget, QLabel

from canvas import Canvas

class View(QMainWindow):
    def __init__(self) -> None:
        super().__init__()

        self.setWindowTitle("My hand-written digit recognizer")
        # 固定大小，可修改
        self.setFixedSize(QSize(720, 720))

        self.mainWidget = MainWidget()
        # 中心组件
        self.setCentralWidget(self.mainWidget)

    # 设置 presenter
    def setPresenter(self, presenter):
        self.mainWidget.setPresenter(presenter)

class MainWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()

        # 画板组件
        self.canvas = Canvas()
        self.presenter = None

        self.clear_btn = QPushButton(text="C  l  e  a  r")

        btn_layout = QHBoxLayout()
        btn_layout.addWidget(self.clear_btn)
        btn_widget = QWidget()
        btn_widget.setLayout(btn_layout)


        self.showLabel = QLabel()
        showLayout = QHBoxLayout()
        showLayout.addWidget(self.showLabel)
        showWidget = QWidget()
        showWidget.setLayout(showLayout)

        main_layout = QVBoxLayout()
        main_layout.addWidget(self.canvas, 10)
        main_layout.addWidget(btn_widget, 1)
        main_layout.addWidget(showWidget, 2)

        self.setLayout(main_layout)

        self.clear_btn.clicked.connect(self.onClearButtonClicked)

        # 样式表
        self._initCss()

    def onClearButtonClicked(self):
        print("Clear button is clicked")
        self.canvas.clear()
        self.showLabel.setText("")

    def onPredictButtonClicked(self):
        print("Predict button is clicked")
        self.canvas.timer.stop()
        if self.presenter is not None:
            self.presenter.onPredictButtonClicked(
                image = self.canvas.getImage(),
                callback = self.onPredict
            )

    def setPresenter(self, presenter):
        self.presenter = presenter

    def onPredict(self, labelList):
        labelList = "".join(filter(str.isdigit, str(labelList)))
        self.showLabel.setText("{}".format(labelList))
        print("I am view.py. Your input is label {}".format(labelList))

    def _initCss(self):
        self.setStyleSheet("""

        """)

        self.clear_btn.setStyleSheet("""
            QPushButton {
                border:none;
                border-radius:10px;
                border:4px solid gray;
                color:gray;
                background: white;
                height: 50px;
                font-family:"Microsoft Yahei";
                font-size: 50px;
                font-weight:bolder;
            }
            QPushButton:hover {
                background: gray;
                color:white;
            }
        """)

        # 展示框设置
        self.showLabel.setAlignment(Qt.AlignCenter)
        self.showLabel.setStyleSheet("""
            QLabel {
                font-family:"Microsoft Yahei";
                font-weight:bolder;
                text-align: center;
                font-size: 80px;
                background: white;
                border-radius:10px;
            }
        """)